package com.shsxt.service;



import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shsxt.dao.CouponDao;
import com.shsxt.dao.WodeDao;
import com.shsxt.model.Activity;
import com.shsxt.vo.CdkeyQuery;

@Service
public class CouponService {
	
	@Autowired
	private WodeDao wodeDao;
	@Autowired
	private WodeService wodeService;
	@Autowired
	private CouponDao couponDao;
    
	public Map<String, Object> activity() {
		Map<String, Object> map = new HashMap<String,Object>();

		List<Activity> activitys=new ArrayList<Activity>();
		 activitys=couponDao.findActivity();
		 map.put("activity", activitys);
		return map;
	}
	public Map<String, Object> activityDianzan(String id,String phoneNumber) {
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("id", id);
		//验证是否点赞过
		 Activity ac= couponDao.yzdianzan(id);
		 String[] phoneNumbers=ac.getPhoneNumbers().split(",");
		 for(String pn:phoneNumbers){
			 if(pn.equals(phoneNumber)){
				 map.put("activity", "失败");
				 return map;
			 }
		 }
		 //去点赞
			param.put("phoneNumber", ac.getPhoneNumbers()+","+phoneNumber);

		 couponDao.dianzan(param);
		 map.put("activity", "成功");
		return map;
	}
	  //兑换 兑换码
	public synchronized  Map<String, Object> usecdkey(String cdkey,String phoneNumber) {
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("phoneNumber", phoneNumber);
		param.put("cdkey", cdkey);

		map.put("0", "失败");
		if(cdkey!=null&&phoneNumber!=null){
			CdkeyQuery cd=couponDao.findCdkey(cdkey);
			if(cd==null||cd.getUseable().equals("已使用")){
				return map;
			}
			
			map.put("0", "成功");
			param.put("memberBalance", Float.valueOf(cd.getCount()));
			couponDao.updateMemberBalance(param);
			couponDao.updateCdkey(param);
		}
		return map;
}
	public Map<String, Object> fixCdkey(String cdkey, String phoneNumber, String check) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String,Object>();
		Map<String,Object> duanxin=new HashMap<String,Object>();
		duanxin=wodeService.duanxin(phoneNumber,"兑换码",check);
		if(duanxin.get("0").equals("成功")){
			param.put("user", phoneNumber);
			param.put("cdkey", cdkey);
			map.put("0", "失败");
			if(cdkey!=null&&phoneNumber!=null){
				CdkeyQuery cd=couponDao.findCdkey(cdkey);
				if(cd==null){
					return map;
				}
				map.put("0", "成功");
				param.put("memberBalance", Float.valueOf(cd.getCount()));
				couponDao.updateMemberBalance(param);
				couponDao.updateCdkey(param);
			}
		}else{
			map.put("0", duanxin.get("0"));
		}
		return map;
	}

	


	
	
	
	
	
	
	
}
